package com.zxl.working.controller;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.zxl.working.entity.admin.SysUser;
import com.zxl.working.service.UserService;
import com.zxl.working.utils.CommUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    private Logger logger = LogManager.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @Resource
    private CommUtil commUtil;

    @PostMapping("/login")
    public String login(HttpServletRequest request, @RequestBody String paramStr){
        return userService.login(paramStr,request);
    }

    @PostMapping("/updatePwd")
    public String updatePwd(HttpServletRequest request, @RequestBody String paramStr){
        return userService.updatePwd(paramStr,request);
    }

    @PostMapping("/serviceLogin")
    public String serviceLogin(HttpServletRequest request, @RequestBody String paramStr){
        return userService.serviceLogin(paramStr,request);
    }

    @PostMapping("/isLogin")
    public String isLogin(HttpServletRequest request){
        if(request.isRequestedSessionIdValid()){
            return commUtil.returnSuccess("session有效");
        }else {
            return commUtil.returnFalse("session无效");
        }
    }

    @PostMapping("/logOut")
    public String logOut(HttpServletRequest request){
        request.getSession().invalidate();

        return commUtil.returnSuccess("登出成功");
    }

}
